package com.piz.service.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.piz.model.Role;
import com.piz.model.User;
import com.piz.model.common.NavItem;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.userservice.UserService;


@Component(value = "navSetUpService")
public class NavSetUpService {
	
	@Autowired
	UserService userService;
	
	private boolean isAdmin = false;
	private boolean isCustomer = false;
	private boolean bpoUser = false;
	private boolean anonymousUser1 = false;
	
	public List<NavItem> fetchTopNavItemsByUsername(String username) throws UnexpectedServiceException
	{
		isAdmin = false;
		 isCustomer = false;
		bpoUser = false;
		anonymousUser1 = false;
		setUserType(username);
		
		List<NavItem> nav = new ArrayList<NavItem>();
		
		if(isAdmin)
		{
			NavItem navItem1 = new NavItem("home", "Home" , "welcome.action");
			NavItem navItem2 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem3 = new NavItem("order", "Order Online" , "selectpizza.action");
			NavItem navItem4 = new NavItem("profile", "Profile" , "registerUser.action?username="+username);
			NavItem navItem5 = new NavItem("contactus", "Contact Us" , "#");
			NavItem navItem6 = new NavItem("aboutus", "About Us" , "#");
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);nav.add(navItem5);nav.add(navItem6);
		}
		else if(bpoUser)
		{
			NavItem navItem1 = new NavItem("home", "Home" , "welcome.action");
			NavItem navItem2 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem3 = new NavItem("order", "Order Online" , "selectpizza.action");
			NavItem navItem4 = new NavItem("profile", "Profile" , "registerUser.action?username="+username);
			NavItem navItem5 = new NavItem("contactus", "Contact Us" , "#");
			NavItem navItem6 = new NavItem("aboutus", "About Us" , "#");
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);nav.add(navItem5);nav.add(navItem6);
		}
		else if(isCustomer)
		{
			NavItem navItem1 = new NavItem("home", "Home" , "welcome.action");
			NavItem navItem2 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem3 = new NavItem("order", "Order Online" , "selectpizza.action");
			NavItem navItem4 = new NavItem("profile", "Profile" , "registerUser.action?username="+username);
			NavItem navItem5 = new NavItem("contactus", "Contact Us" , "#");
			NavItem navItem6 = new NavItem("aboutus", "About Us" , "#");
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);nav.add(navItem5);nav.add(navItem6);
		}
		
		else if(anonymousUser1)
		{
			NavItem navItem1 = new NavItem("home", "Home" , "welcome.action");
			NavItem navItem2 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem3 = new NavItem("register", "New User" , "registerUser.action");
			NavItem navItem4 = new NavItem("contactus", "Contact Us" , "#");
			NavItem navItem5 = new NavItem("aboutus", "About Us" , "#");
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);nav.add(navItem5);
		}
		
		return nav;
	}
	
	public List<NavItem> fetchLeftNavItemsByUsername(String username) throws UnexpectedServiceException
	{
		isAdmin = false;
		 isCustomer = false;
		bpoUser = false;
		anonymousUser1 = false;
		setUserType(username);
		
		List<NavItem> nav = new ArrayList<NavItem>();
		
		 if(isAdmin)
		{
			NavItem navItem1 = new NavItem("productMgmt", "Product Management" , "productList.action");
			NavItem navItem2 = new NavItem("catMgmt", "Category Management" , "categoryList.action");
			NavItem navItem3 = new NavItem("bpoMgmt", "BPO Management" , "#");
			
			NavItem item3SubItem1 = new NavItem("bpocenters", "BPO Centers" , "#");
			NavItem item3SubItem2 = new NavItem("bpousers", "BPO Users" , "#");
			List<NavItem> item3SubItems = new ArrayList<NavItem>();
			item3SubItems.add(item3SubItem1); item3SubItems.add(item3SubItem2);
			navItem3.setSubItems(item3SubItems);
			
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);
		}
		else if(bpoUser)
		{
			NavItem navItem1 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem2 = new NavItem("order", "Order Online" , "selectpizza.action");
			NavItem navItem3 = new NavItem("searchUser", "Search User" , "#");
			NavItem navItem4 = new NavItem("searchProduct", "Search Product" , "#");
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);
		}
		else if(isCustomer)
		{
			NavItem navItem1 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem2 = new NavItem("order", "Order Online" , "selectpizza.action");
			NavItem navItem3 = new NavItem("Offer", "Download Coupons" , "#");
			NavItem navItem4 = new NavItem("profile", "Profile" , "registerUser.action?username="+username);
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);
		}
		else if(anonymousUser1)
		{
			NavItem navItem1 = new NavItem("bpos", "BPO Centers" , "#");
			NavItem navItem2 = new NavItem("register", "New User" , "registerUser.action");
			NavItem navItem3 = new NavItem("order", "Order Online" , "selectpizza.action");
			NavItem navItem4 = new NavItem("offer", "Current Offers" , "#");
			nav.add(navItem1);nav.add(navItem2);nav.add(navItem3);nav.add(navItem4);
		}
		
		return nav;
	}
	
	public void setUserType(String username)
	{
		User user = userService.findUserByUsername(username);
		
		
		if(user != null && user.getRoles() != null && user.getRoles().size() > 0)
		{
			for(Role role : user.getRoles())
			{
				if(role.getName().equalsIgnoreCase("admin"))
				{
					isAdmin = true;
					break;
				}
				if(role.getName().equalsIgnoreCase("bpouser"))
				{
					bpoUser = true;
					break;
				}
			}
			if(!isAdmin  && !bpoUser)
			{
				isCustomer = true;
			}
		}
		else
		{
			anonymousUser1 = true;
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	public boolean isCustomer() {
		return isCustomer;
	}

	public void setCustomer(boolean isCustomer) {
		this.isCustomer = isCustomer;
	}

	public boolean isBpoUser() {
		return bpoUser;
	}

	public void setBpoUser(boolean bpoUser) {
		this.bpoUser = bpoUser;
	}

	public boolean isAnonymousUser1() {
		return anonymousUser1;
	}

	public void setAnonymousUser1(boolean anonymousUser1) {
		this.anonymousUser1 = anonymousUser1;
	}

	

}
